#!/usr/bin/env python

#
# file2graphtec
#
# Send a file to a Graphtec vinyl cutter using libusb
#
# Copyright (c) 2013 Peter Monta <pmonta@gmail.com>
#

import sys
import usb1

#
# open a graphtec vinyl cutter from a list of recognized devices
#

device_list = [
  (0x0b4d, 0x1121),     # Silhouette Cameo
  (0x0b4d, 0x1123),     # Silhouette Portrait
  (0x0b4d, 0x1132)      # Silhouette Portrait 2
]

def open_graphtec_device(ctx):
  for (vendor_id,product_id) in device_list:
    dev = ctx.openByVendorIDAndProductID(vendor_id, product_id)
    if dev:
      return dev
  return None

#
# main program
#

if len(sys.argv)==2:
  f = open(sys.argv[1], 'rb')
elif len(sys.argv)==1:
  f = sys.stdin
else:
  print 'usage: file2graphtec [filename]'
  sys.exit(1)

endpoint = 1
ctx = usb1.USBContext()

dev = open_graphtec_device(ctx)

if not dev:
  sys.stderr.write('no graphtec device found\n')
  sys.exit(1)

dev.claimInterface(0)

while True:
  data = f.read(8)
  if not data:
    break
  dev.bulkWrite(endpoint, data)

f.close()
